/*
 *  This file is part of Cube.
 *
 *  Cube is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  Cube is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with Cube.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

package cube.charts;

import cube.data.AccelerationDataVector;
import java.util.ArrayList;
import java.util.Hashtable;
import javax.swing.JDesktopPane;
import javax.swing.JInternalFrame;

/**
 *
 * @author akermanl
 */
public class ChartManager extends JInternalFrame {
    
    private static ChartManager instance;   
    private Hashtable<String, JInternalFrame> chartHashtable = new Hashtable<String, JInternalFrame>();
    private int anglesCounter = 1;
    private double rollAverage = 0;
    private double pitchAverage = 0;
    private int count = 0;
    
    private ChartManager(){                       
        // Angles     
        chartHashtable.put("ANDX", new DialChart("Roll (x)",0,0,300,175,-90,90));
        chartHashtable.put("ANDY", new DialChart("Pitch (y)",315,0,300,175,-90,90));
        chartHashtable.put("ANDZ", new DialChart("Yaw (z)",630,0,300,175,-90,90));                 
        chartHashtable.put("ANL", new LineChart("Roll, Pitch & Yaw",370,180,565,355, "degree"));
        ((LineChart)chartHashtable.get("ANL")).addAxis("roll","Roll");
        ((LineChart)chartHashtable.get("ANL")).addAxis("pitch","Pitch");
        ((LineChart)chartHashtable.get("ANL")).addAxis("yaw","Yaw");       
        chartHashtable.put("ANC", new CubeChart(this,"Cube",0,180,355,355));
        
        // Accelerometer
        chartHashtable.put("ACDXN", new DialChart("X-Axis [g]",0,0,300,175,-2,2));
        chartHashtable.put("ACDX1", new DialChart("X-Axis [g] - first order",0,180,300,175,-2,2));
        chartHashtable.put("ACDX2", new DialChart("X-Axis [g] - second order",0,360,300,175,-2,2));
        chartHashtable.put("ACDYN", new DialChart("Y-Axis [g]",315,0,300,175,-2,2));
        chartHashtable.put("ACDY1", new DialChart("Y-Axis [g] - first order",315,180,300,175,-2,2));
        chartHashtable.put("ACDY2", new DialChart("Y-Axis [g] - second order",315,360,300,175,-2,2));
        chartHashtable.put("ACDZN", new DialChart("Z-Axis [g]",630,0,300,175,-2,2));
        chartHashtable.put("ACDZ1", new DialChart("Z-Axis [g] - first order",630,180,300,175,-2,2));
        chartHashtable.put("ACDZ2", new DialChart("Z-Axis [g] - second order",630,360,300,175,-2,2));
        chartHashtable.put("ACL",   new LineChart("X-, Y- & Z-Axis [g]",0,180,930,355, "g")); 
        ((LineChart)chartHashtable.get("ACL")).addAxis("x","X-Axis");
        ((LineChart)chartHashtable.get("ACL")).addAxis("y","Y-Axis");
        ((LineChart)chartHashtable.get("ACL")).addAxis("z","Z-Axis");
    }
    
     public static ChartManager getInstance () {
         if (ChartManager.instance == null) {
             ChartManager.instance = new ChartManager ();
         }
         return ChartManager.instance;
     }

    
    public void resetPositions() {
       /* for (int j=0; j<=listOfCharts.size()-1; j++){
            getHashtableOfCharts().get(listOfCharts.get(j).getId()).setBounds(listOfCharts.get(j).getXPos(), listOfCharts.get(j).getYPos(), listOfCharts.get(j).getWidth(), listOfCharts.get(j).getHeight());
        }*/
     }
    
     public void resetValues(){
         for (JInternalFrame jInternalFrame : getHashtableOfCharts().values() ) {
             ((Chart) jInternalFrame).resetValues();
         }
     }
 
     public void updateVisibility(JDesktopPane jDesktopPane, String name, boolean selected){         
        if (selected) {     
            if (getHashtableOfCharts().get(name).isVisible()!=true){
                jDesktopPane.add(getHashtableOfCharts().get(name));
                getHashtableOfCharts().get(name).setVisible(true);  
            }        
        }
        else {      
                getHashtableOfCharts().get(name).setVisible(false);  
                jDesktopPane.remove(getHashtableOfCharts().get(name));
                  
        }         
     }
            
     public void updateValues(ArrayList<AccelerationDataVector> accelerationDataVectorList){   
         
         double x = accelerationDataVectorList.get(accelerationDataVectorList.size()-1).getX();
         double y = accelerationDataVectorList.get(accelerationDataVectorList.size()-1).getY();
         double z = accelerationDataVectorList.get(accelerationDataVectorList.size()-1).getZ();
         double roll = accelerationDataVectorList.get(accelerationDataVectorList.size()-1).getRoll();
         double pitch = accelerationDataVectorList.get(accelerationDataVectorList.size()-1).getPitch();
                             
         ((DialChart) getHashtableOfCharts().get("ACDXN")).setValue(x);
         ((DialChart) getHashtableOfCharts().get("ACDYN")).setValue(y);
         ((DialChart) getHashtableOfCharts().get("ACDZN")).setValue(z);
         
         ((DialChart) getHashtableOfCharts().get("ACDX1")).setValue(x*0.9);
         ((DialChart) getHashtableOfCharts().get("ACDY1")).setValue(y*0.9);
         ((DialChart) getHashtableOfCharts().get("ACDZ1")).setValue(z*0.9);
         
         ((DialChart) getHashtableOfCharts().get("ACDX2")).setValue(x*0.8);
         ((DialChart) getHashtableOfCharts().get("ACDY2")).setValue(y*0.8);
         ((DialChart) getHashtableOfCharts().get("ACDZ2")).setValue(z*0.8);
         
         ((DialChart) getHashtableOfCharts().get("ANDX")).setValue(roll);
         ((DialChart) getHashtableOfCharts().get("ANDY")).setValue(pitch);
         ((DialChart) getHashtableOfCharts().get("ANDZ")).setValue(0);
         
         ((LineChart) getHashtableOfCharts().get("ANL")).updateValue("roll", count, roll);
         ((LineChart) getHashtableOfCharts().get("ANL")).updateValue("pitch", count, pitch);
         ((LineChart) getHashtableOfCharts().get("ANL")).updateValue("yaw", count, 0);
         
         ((LineChart) getHashtableOfCharts().get("ACL")).updateValue("x", count, x);
         ((LineChart) getHashtableOfCharts().get("ACL")).updateValue("y", count, y);
         ((LineChart) getHashtableOfCharts().get("ACL")).updateValue("z", count, z);

         count+=1;
                
         if (anglesCounter<10){           
             rollAverage+=roll;
             pitchAverage+=pitch;
             anglesCounter+=1;
         } else if (anglesCounter==10){
             rollAverage=rollAverage/10;
             pitchAverage=pitchAverage/10;             
             ((CubeChart) getHashtableOfCharts().get("ANC")).setAngles(rollAverage, pitchAverage, 0.05);  
             anglesCounter = 0;
             pitchAverage = 0;
             rollAverage = 0;
         }  
     }
    
    public Hashtable<String, JInternalFrame> getHashtableOfCharts() {
        return chartHashtable;
    }    

    public void setCharts(Hashtable<String, JInternalFrame> charts) {
        this.chartHashtable = charts;
    }
}
